import {
  selectDriverPaperList,
  selectDriverPaperDetail,
  selectDriverPaperInfo,
  saveDriverPaper,
  selectFileList,
  saveDriverPaperInfo,
  deletePartsInfo,
  delPapers,
  generatedBills,
  selectAll,
} from '@/services/fleetManage/driverPaper';
import prompt from '@/components/Prompt';
import { getPageSize } from '@/utils/common';

export default {
  namespace: 'driverPaper',

  state: {
    driverPaperList: {}, // 司机证件列表
    driverPaperDetail: {}, // 司机证件详情
    formValues: {},
  },

  effects: {
    *selectAll({ payload, callback }, { call }) {
      const response = yield call(selectAll, payload);
      if (response.code === 0) {
        prompt({ content: `账单生成${response.message}!` });
        if (callback) callback(response.data);
      } else {
        if (callback) callback();
      }
    },
    /**
     * 查询司机证件列表
     */
    *selectDriverPaperList({ payload, callback }, { call, put }) {
      const response = yield call(selectDriverPaperList, payload);
      const { code, data } = response;
      let pagination = { list: [], pageSize: getPageSize(), total: 0, pageNum: 1 };
      if (code === 0) {
        pagination = { ...pagination, ...data };
      }
      const { pageNum, pageSize, total, list } = pagination;
      yield put({
        type: 'saveAllValus',
        payload: {
          driverPaperList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    /**
     * 查询司机证件详情
     */
    *selectDriverPaperDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectDriverPaperDetail, payload);
      const { code, data } = response;
      let newData = [];
      if (code === 0) {
        newData = data;
        const filePayLoad = { bizId: payload.id, fileBizType: 'PAPERS' };
        const fileResponse = yield call(selectFileList, filePayLoad);
        if (fileResponse.code === 0) {
          newData.fileList = fileResponse.data;
        } else {
          newData.fileList = [];
        }
      }
      yield put({
        type: 'detail',
        payload: { driverPaperDetail: { [payload.id]: newData } },
      });
      callback && callback(data);
    },
    /**
     * 新增编辑基本信息
     */
    *saveDriverPaper({ payload, callback }, { call }) {
      const response = yield call(saveDriverPaper, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },
    //删除证件：
    *delPapers({ payload, callback }, { call }) {
      const response = yield call(delPapers, payload);
      const { code, data, message } = response;
      const content = `删除${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback();
    },

    /**
     * 生成账单
     */
    *generatedBills({ payload, callback }, { call }) {
      const response = yield call(generatedBills, payload);
      const { code, message, data } = response;
      if (code === 0) {
        prompt({ content: `账单生成${message}!` });
        if (callback) callback(data);
      }
    },

    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    detail(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...state[param], ...payload[param] },
      };
    },
  },
};
